package com.itic.mall.mobile.market;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.ValueHelper;
import com.itic.mall.mobile.base.BaseMobileInterface;
import com.itic.mall.system.goods.goodsspu.persistence.model.GoodsSpu;
import com.itic.mall.system.goods.goodsspu.persistence.model.GoodsSpuExample;
import com.itic.mall.system.goods.goodsspu.persistence.model.GoodsSpuExample.Criteria;
import com.itic.mall.system.market.marketactivity.persistence.model.MarketActivity;
import com.itic.mall.system.market.marketadvert.persistence.model.MarketAdvert;
import com.itic.mall.system.market.marketbanner.persistence.model.MarketBanner;
import com.itic.mall.system.market.wordtag.persistence.model.WordTag;
import com.itic.mall.system.market.wordtag.persistence.model.WordTagExample;
import com.itic.mall.system.market.wordtag.service.WordTagService;

/**
 * 首页模块接口信息
 * 
 * @author 许小宝
 * @time 2017/12/01
 */
@Controller
@RequestMapping(value = "/mobile/market")
public class MarketInterface extends BaseMobileInterface {

	@Autowired
	private WordTagService wordTagService;

	/**
	 * APP首页大分类搜索
	 * 
	 * @return
	 */
	@RequestMapping(value = "/wordtags")
	@ResponseBody
	public Map<String, Object> wordtags(HttpServletRequest request, HttpServletResponse response,String type) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		try {
			
			WordTagExample example = new WordTagExample();
			example.setOrderByClause("REORDER ASC");
			com.itic.mall.system.market.wordtag.persistence.model.WordTagExample.Criteria criteria = example.createCriteria();
			criteria.andIsEffectiveEqualTo(DBConst.TRUE);
			criteria.andIsVisibleEqualTo(DBConst.TRUE);
			
			if( StringHelper.isNotBlank(type) ){
				criteria.andTypeEqualTo(type);
			}
			
			List<WordTag> list = wordTagService.list(example);
			
			resultMap.put(DATA, ValueHelper.tranList2MapList(list, new String[] { "id","note", "reorder","createUser", "isEffective", "updateTime", "orgCode", "isVisible", "createTime", "type" }));
			errorCode = SUCCESS_ERROR_CODE;
			errorMsg = SUCCESS_ERROR_MSG;
		} catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("GoodsInterface", "categorys", e.toString(), "APP首页大分类搜索", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
	}
	
	
	/**
	 * 轮播图详情接口
	 * 
	 * @return
	 */
	@RequestMapping(value = "/advertDetail")
	@ResponseBody
	public Map<String, Object> advertDetail(HttpServletRequest request, HttpServletResponse response, String id ) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		try {
			if( StringHelper.isNotBlank(id) ){
				MarketAdvert advert = marketAdvertService.get(id);
			    resultMap.put(DATA, getSpuByIds(request,advert.getSpuIds(),advert.getBanner(),advert.getName()));
				errorCode = SUCCESS_ERROR_CODE;
				errorMsg = SUCCESS_ERROR_MSG;
			}
		} catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("GoodsInterface", "categorys", e.toString(), "APP首页大分类搜索", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
	}
	
	/**
	 * 轮播图详情接口
	 * 
	 * @return
	 */
	@RequestMapping(value = "/activityDetail")
	@ResponseBody
	public Map<String, Object> activityDetail(HttpServletRequest request, HttpServletResponse response, String id ) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		try {
			if( StringHelper.isNotBlank(id) ){
				MarketActivity activity = marketActivityService.get(id);
			    resultMap.put(DATA, getSpuByIds(request,activity.getSpuIds(),activity.getBanner(),activity.getName()));
				errorCode = SUCCESS_ERROR_CODE;
				errorMsg = SUCCESS_ERROR_MSG;
			}
		} catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("GoodsInterface", "categorys", e.toString(), "APP首页大分类搜索", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
	}
	
	/**
	 * 轮播图详情接口
	 * 
	 * @return
	 */
	@RequestMapping(value = "/bannerDetail")
	@ResponseBody
	public Map<String, Object> modelDetail(HttpServletRequest request, HttpServletResponse response, String id ) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		try {
			if( StringHelper.isNotBlank(id) ){
				MarketBanner banner = marketBannerService.get(id);
			    resultMap.put(DATA, getSpuByIds(request,banner.getSpuIds(),banner.getBanner(),banner.getName()));
				errorCode = SUCCESS_ERROR_CODE;
				errorMsg = SUCCESS_ERROR_MSG;
			}
		} catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("GoodsInterface", "categorys", e.toString(), "APP首页大分类搜索", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
	}
	
	/**
	 * 轮播图详情接口
	 * 
	 * @return
	 */
	@RequestMapping(value = "/newyearDetail")
	@ResponseBody
	public Map<String, Object> newyearDetail(HttpServletRequest request, HttpServletResponse response, String id ) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		try {
			if( StringHelper.isNotBlank(id) ){
				MarketBanner banner = marketBannerService.get(id);
				if( null != banner ){
					resultMap.put(DATA, getNewYearSpuByIds(request,banner.getSpuIds(),banner.getBanner(),banner.getName()));
					errorCode = SUCCESS_ERROR_CODE;
					errorMsg = SUCCESS_ERROR_MSG;
				}else{
					MarketActivity activity = marketActivityService.get(id);
					if( null != activity ){
						 resultMap.put(DATA, getNewYearSpuByIds(request,activity.getSpuIds(),activity.getBanner(),activity.getName()));
						 errorCode = SUCCESS_ERROR_CODE;
						 errorMsg = SUCCESS_ERROR_MSG;
					}else{
						 MarketAdvert advert = marketAdvertService.get(id);
					     resultMap.put(DATA, getNewYearSpuByIds(request,advert.getSpuIds(),advert.getBanner(),advert.getName()));
						 errorCode = SUCCESS_ERROR_CODE;
						 errorMsg = SUCCESS_ERROR_MSG;
					}
				}
			}
		} catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("GoodsInterface", "categorys", e.toString(), "APP首页大分类搜索", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
	}
	
	
	
	
	private Map<String, Object> getSpuByIds(HttpServletRequest request, String spuIds, String banner, String title ) {
		ArrayList<String> ids = new ArrayList<String>();
		if( StringHelper.isNotBlank(spuIds) ){
			String[] split = spuIds.split(",");
			for( int i=0; i<split.length; i++ ){
				ids.add(split[i]);
			}
		}
		
		Map<String, Object> dataMap = new HashMap<String, Object>();
		GoodsSpuExample example = new GoodsSpuExample();
		Criteria criteria = example.createCriteria();
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);// 是否有效
		criteria.andIsShowEqualTo(DBConst.TRUE);// 是否可见
		criteria.andIdIn(ids);
		this.handlePageQueryDefault(request, example);// 分页
		int count = goodsSpuService.count(example);// 总条数
		List<GoodsSpu> list = goodsSpuService.list(example);// 数据list
		
		dataMap.put("banner", banner.split(","));
		dataMap.put("title", title);
		dataMap.put("total", count);
		dataMap.put("isMore", isMore(request, count));
		dataMap.put("list", ValueHelper.tranList2MapList(list,spuIgnoreProperties));

		return dataMap;
	}
	
	
	private Map<String, Object> getNewYearSpuByIds(HttpServletRequest request, String spuIds, String banner, String title ) {
		ArrayList<String> idsSix = new ArrayList<String>();
		if( StringHelper.isNotBlank(spuIds) ){
			String[] split = spuIds.split(",");
			for( int i=0; i<6; i++ ){
				idsSix.add(split[i]);
			}
		}
		
		ArrayList<String> idsList = new ArrayList<String>();
		if( StringHelper.isNotBlank(spuIds) ){
			String[] split = spuIds.split(",");
			for( int i=6; i<split.length; i++ ){
				idsList.add(split[i]);
			}
		}
		
		Map<String, Object> dataMap = new HashMap<String, Object>();
		GoodsSpuExample example = new GoodsSpuExample();
		Criteria criteria = example.createCriteria();
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);// 是否有效
		criteria.andIsShowEqualTo(DBConst.TRUE);// 是否可见
		criteria.andIdIn(idsList);
		this.handlePageQueryDefault(request, example);// 分页
		int count = goodsSpuService.count(example);// 总条数
		List<GoodsSpu> list = goodsSpuService.list(example);// 数据list
		
		dataMap.put("banner", "http://file.jhjvip.cn/banner.jpg");
		dataMap.put("advert", "http://file.jhjvip.cn/advert.jpg");
		dataMap.put("title", title);
		dataMap.put("total", count);
		dataMap.put("isMore", isMore(request, count));
		dataMap.put("six", getBySpuIds(idsSix));
		dataMap.put("list", ValueHelper.tranList2MapList(list,spuIgnoreProperties));

		return dataMap;
	}
	
	public List<Map<String, Object>> getBySpuIds( List<String>  ids ){
		GoodsSpuExample example = new GoodsSpuExample();
		Criteria criteria = example.createCriteria();
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);// 是否有效
		criteria.andIsShowEqualTo(DBConst.TRUE);// 是否可见
		criteria.andIdIn(ids);
		List<GoodsSpu> list = goodsSpuService.list(example);// 数据list
		List<Map<String, Object>> tranList2MapList = ValueHelper.tranList2MapList(list,spuIgnoreProperties);
		return tranList2MapList;
	}
}